//
//  SVGAnimationElement.m
//  Shake and Bake
//
//  Created by Antoine Quint on 09/10/07.
//  Copyright 2007 __MyCompanyName__. All rights reserved.
//

#import "SVGAnimationElement.h"

@implementation SVGAnimationElement

- (id)initWithAttributeDict:(NSDictionary *)attributesDict andParent:(CALayer *)parentElement
{
  [super init];
	if (self) {
    self.keyPath = [attributesDict objectForKey:@"attributeName"];
    // first, set up animation attritbutes
    [self initAnimationProperties:attributesDict];		
    // then find the target layer
    // XXX: for now, we don't have lookup by name, so add parent always
  	[parentElement addAnimation:self forKey:nil];
	}
	//NSLog(@"Added animation to element with name %@", parentElement.name);	
	return self;
}

- (void)initAnimationProperties:(NSDictionary *)attributes {

	NSString *attribute;

  // make sure fill="freeze" has a chance of working
  self.removedOnCompletion = NO;

	// "begin" attribute
	attribute = [attributes objectForKey:@"begin"];
	if (attribute != nil) {
    self.beginTime = CACurrentMediaTime() + [attribute floatValue];
    //NSLog(@"begin: %f", self.beginTime);
	}

  // "dur" attribute
  self.duration = [[attributes objectForKey:@"dur"] floatValue];
  //NSLog(@"duration: %f", self.duration);

  // "fill" attribute
	attribute = [attributes objectForKey:@"fill"];
  if (attribute != nil && [attribute isEqualToString:@"freeze"]) {
      self.fillMode = kCAFillModeForwards;
      self.removedOnCompletion = FALSE;
    //NSLog(@"fill: %@", self.fillMode);
  }

  // "accumulate" attribute
	attribute = [attributes objectForKey:@"accumulate"];
  self.cumulative = (attribute != nil && [attribute isEqualToString:@"sum"]);

  // "accumulate" attribute
	attribute = [attributes objectForKey:@"additive"];
  self.additive = (attribute != nil && [attribute isEqualToString:@"sum"]);

  // repeatCount attribute
	attribute = [attributes objectForKey:@"repeatCount"];
	self.repeatCount = (attribute == nil) ? 0 : [attribute floatValue];
  //NSLog(@"repeatCount: %f", self.repeatCount);

  // repeatDur attribute
	attribute = [attributes objectForKey:@"repeatDur"];
	self.repeatDuration = (attribute == nil) ? 0 : [attribute floatValue];  
  //NSLog(@"repeatDuration: %f", self.repeatDuration);

}

@end
